package com.example.ai.controller;


import com.example.ai.entity.vo.MessageVO;
import com.example.ai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {
    private final ChatHistoryRepository chatHistoryRepository;
    private final ChatMemory chatMemory;
    //获取某类型的全部窗口
    @GetMapping("{type}")
    public List< String> getChatIds(@PathVariable("type") String type){
        return chatHistoryRepository.getChatIds(type);
    }
    //获取聊天记录
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable String type,@PathVariable String chatId){
        List<Message> messages = chatMemory.get(chatId);
        if (messages== null){
            return null;
        }
        return messages.stream().map(m -> new MessageVO(m)).toList();
    }
    }
